package com.bite.friend.service.file.impl;

import com.bite.common.core.enums.ResultCode;
import com.bite.common.file.domain.OSSResult;
import com.bite.common.file.service.OSSService;
import com.bite.common.security.exception.ServiceException;
import com.bite.friend.service.file.IFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
public class FileServiceImpl implements IFileService {

    @Autowired
    private OSSService ossService;

    @Override
    public OSSResult upload(MultipartFile multipartFile) {
        if (multipartFile == null) {
            throw new ServiceException(ResultCode.FAILED_FILE_EMPTY);
        }
        OSSResult ossResult = null;
        try {
            ossResult = ossService.uploadFile(multipartFile);
        } catch (Exception e) {
            log.error("文件上传异常 e：{}", e.toString());
            throw new ServiceException(ResultCode.ERROR);
        }
        return ossResult;
    }
}
